package com.game.astar
{
	import flash.utils.ByteArray;

	public class MapFloor
	{
		public var mapId:int;
		public var mapWidth:int;
		public var mapHeight:int;
		
		private var _maskStepX:int;
		private var _maskStepY:int;
		private var _maskReady:Boolean = false;
		private var _maskByte:ByteArray;
		
		private var _wallStepX:int;
		private var _wallStepY:int;
		private var _wallReady:Boolean = false;
		private var _wallByte:ByteArray;
		
		public static var isNewMapData:Object = {15:true,99:true,103:true,1000:true,1001:true,1002:true,1003:true,1004:true,1005:true,1006:true,227:true,326:true};
		
		public function MapFloor() {
			
		}
		
		public function setMaskData(byte:ByteArray):void {
			mapId = byte.readInt();
			mapWidth = byte.readInt();
			mapHeight = byte.readInt();
			_maskStepX = byte.readInt();
			_maskStepY = byte.readInt();
			byte.readInt();
			_maskByte = new ByteArray();
			byte.readBytes(_maskByte, 0, byte.bytesAvailable);
			_maskByte.uncompress();
			_maskReady = true;
			return;
		}
		
		public function setWallData(byte:ByteArray):void {
			mapId = byte.readInt();
			mapWidth = byte.readInt();
			mapHeight = byte.readInt();
			_wallStepX = byte.readInt();
			_wallStepY = byte.readInt();
			byte.readInt();
			_wallByte = new ByteArray();
			byte.readBytes(_wallByte, 0, byte.bytesAvailable);
			_wallByte.uncompress();
			_wallReady = true;
			return;
		}
		
		public function clear():void {
			_wallReady = false;
			_maskReady = false;
			_wallByte = null;
			_maskByte = null;
			mapId = -1;
		}
		
		public function canWalk( pixX:int , pixY:int ):Boolean {
			return !isWall(pixX,pixY);
		} 
		
		public function isWall( pixX:int , pixY:int ):Boolean {
			if(false == _wallReady) {
				return true;
			}
			else if( !_wallByte ) {
				return true;
			}
			else if(pixX < 0 || pixX > mapWidth || pixY < 0 || pixY > mapHeight) {
				return true;
			} 
			
			var checkX : int = pixX/_wallStepX;
			var checkY : int = pixY/_wallStepY;
			var index:int = checkY * int(mapWidth/_wallStepX) + checkX;
			if(isNewMapData[mapId]){
				return Boolean(_wallByte[index]);
			}else{
				var index_array:int = index / 8;
				var index_bit:int = index % 8;
				var mask:int = 1<<index_bit;
				var ori:int = _wallByte[index_array] ;
				var result:int = ori & mask;		
				return Boolean(result);
			}
		}
		
		public function isMask( pixX:uint , pixY:uint ):Boolean {
			if(false == _maskReady) {
				return false;
			} else if( pixX < 0 || pixX > mapWidth || pixY < 0 || pixY > mapHeight ){
				return false;
			}else if( !_maskByte ) {
				return false;
			}
			
			var checkX : int = pixX/_maskStepX;
			var checkY : int = pixY/_maskStepY;
			var index:int = checkY * int(mapWidth/_maskStepX) + checkX;
			if(isNewMapData[mapId]){
				return Boolean(_maskByte[index]);
			}else{
				var index_array:int = index / 8;
				var index_bit:int = index % 8;
				var mask:uint = 1 << index_bit;
				var ori:uint = _maskByte[index_array] ;
				var result:uint = ori & mask;		
				return Boolean(result);
			}
		} 
	}
}